<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%String path = request.getContextPath();%>
<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c" %>
<%@ taglib uri="/WEB-INF/tld/ui.tld" prefix="ui" %>
<%@ taglib uri="/WEB-INF/tld/sys.tld" prefix="sys" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="renderer" content="webkit">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>项目列表</title>
    <jsp:include page="../../import.jsp" flush="true"/>
    <!-- 是否显示提交按钮 -->
    <sys:funcAuth resCd="LinkSubmit">
        <span id="isShowCmtBtn"/>
    </sys:funcAuth>
    <!-- 是否显示审核按钮 -->
    <sys:funcAuth resCd="LinkCheck">
        <span id="isShowAudBtn"/>
    </sys:funcAuth>
    <sys:funcAuth resCd="LinkCrAmt">
        <span id="isShowLinkCrAmtBtn"/>
    </sys:funcAuth>
</head>
<body class="iframeBody">
<div class="Wrapper">
    <div id="pageInTab" class="pageInTab">
        <div tabid="tabA" title="项目列表" lselected="true">
            <div class="searchArea">
                <form id="searchBar">
                    <div class="fieldset1 clearFix">
                        <div class="fieldBox">
                            <div class="label">挂靠公司名称</div>
                            <div class="field">
                                <input name="lnkCompNm" id="lnkCompNm" type="text"
                                       class="ui-textbox" value="">
                            </div>
                        </div>
                        <div class="fieldBox">
                            <div class="label">审核状态</div>
                            <div class="field">
                                <select name="audStsCd" id="audStsCd" data-value="">
                                    <option value="  " selected="selected">--请选择--</option>
                                    <c:forEach items="${keyValueList}" var="item">
                                        <option value="${item.key}">${item.value }</option>
                                    </c:forEach>
                                </select>
                            </div>
                        </div>
                        <div class="clearFix"></div>
                        <div class="btnsBox">
                            <span id="searchBtn" class="sq-button sq-button-round">查询</span>
                            <span id="clear" onclick="clearBtn();" class="sq-button sq-button-round">清空</span>
                            <span id="addBtn" onclick="addBefore();" class="sq-button sq-button-round">新增</span>
                            <span id="flowStartBtn" onclick="flowStartBtn();"
                                  class="sq-button sq-button-round">发起流程</span>
                        </div>
                    </div>
                </form>

            </div>
            <div id="ligergrid1" class="sq-grid"></div>
        </div>
        <div tabid="tabB" title="流程跟踪" id="tabBId">
            <iframe frameborder="0" name="tabB" id="tabB" src=""></iframe>
        </div>
        <!-- <div tabid="tabC" title="变更记录" id="tabCId">
            <iframe frameborder="0" name="tabC" id="tabC" src=""></iframe>
        </div> -->
    </div>
</div>

<!--新增挂靠公司时，展示新增页面-->
<div id="div_lnk_comp" style="display: none">
    <form id="div_lnk_comp_form">
        <div class="fieldset1 tabel_col tabel_col_s clearFix noBorder">
            <div class="fieldBox col_50">
                <div class="label">公司名称</div>
                <div class="field">
                    <input name="lnkCompNmBk" id="lnkCompNmBk" type="text"
                           class="ui-textbox" value="" validate="{required:true}"> <span
                        class="requiredSymbol">*</span>
                </div>
            </div>
            <div class="fieldBox col_50">
                <div class="label">组织机构代码</div>
                <div class="field">
                    <input name="orgCode" id="orgCode" type="text" class="ui-textbox"
                           value="" validate="{required:true}"> <span
                        class="requiredSymbol">*</span>
                </div>
            </div>
            <div class="fieldBox col_50">
                <div class="label">营业执照注册号</div>
                <div class="field">
                    <input name="bizLicNo" id="bizLicNo" type="text" isShow="isother"
                           class="ui-textbox" value="" validate="{required:true}"> <span
                        class="requiredSymbol" isShow="isother">*</span>
                </div>
            </div>
            <div class="fieldBox col_50">
                <div class="label">税务登记号</div>
                <div class="field">
                    <input name="taxRegNo" id="taxRegNo" type="text"
                           class="ui-textbox" value="" validate="{required:true}"> <span
                        class="requiredSymbol">*</span>
                </div>
            </div>
        </div>
    </form>
</div>
<script type="text/javascript">
    var grid = null;
    var buOrgCd = '${buOrgCd}';
    var pageInApp = {}; // to 下一级子窗口 接口
    var pageInTab = null;

    $(function () {
        initTab();
        $("#ligergrid1").ligerGrid({
            url: '<%=path%>/project/prjlnkcomp/initList.do',
            method: 'get',
            columns: [
                {
                    display: '公司名称', name: 'lnkCompNm', minWidth: 60,
                    render: function (rowdata, rowindex, value, column) {
                        var h = '';
                        h = '<a  href="javascript:detailBtn(';
                        h += "'" + rowdata.id + "'";
                        h += ')">' + value + '</a>'
                        return h;
                    }
                },
                {display: '道路运输许可证有效期', name: 'expDt', render: renderDate, minWidth: 140},
                {display: '成立日期', name: 'regDt', minWidth: 140, render: renderDate, enabledSort: true},
                {display: '企业类型', name: 'compTyp', minWidth: 60},
                {display: '法人代表', name: 'corpRep', minWidth: 140},
                {display: '联系人', name: 'lnkNm', minWidth: 140},
                {display: '在我司累积挂靠车辆', name: 'ourTotLnkQyt', minWidth: 140},
                {
                    display: '所属机构', name: 'buOrgCdNms', align: 'left', width: 200, isEllipsis: false,
                    render: function (rowdata, rowindex, value) {
                        if (StringUtils.isEmpty(value)) {
                            return "";
                        } else {
                            var h = '<span class="gridTip" title="' + value + '">' + value + '</span>'
                            return h;
                        }
                    }
                },
                {
                    display: '审核状态', minWidth: 140, render: function (rowdata, rowindex, value) {
                    if (rowdata && rowdata.sysFlowSHDTOList && rowdata.sysFlowSHDTOList.length > 2) {
                        return;
                    } else {
                        return rowdata.audStsCdNm;
                    }
                }
                },
                {
                    display: '流程编号', minWidth: 220, isEllipsis: false, render: function (rowdata, rowindex, value) {
                    if (rowdata && rowdata.sysFlowSHDTOList && rowdata.sysFlowSHDTOList.length == 1) {
                        var pno = rowdata.sysFlowSHDTOList[0].pno;
                        var audStsCd = rowdata.sysFlowSHDTOList[0].audStsCd;
                        var pinstId = rowdata.sysFlowSHDTOList[0].pinstId;
                        var dataId = rowdata.sysFlowSHDTOList[0].dataId;
                        if (StringUtils.isNotEmpty(pinstId)
                            && StringUtils.isNotEmpty(audStsCd)
                            && StringUtils.isNotEmpty(dataId)
                            && StringUtils.isNotEmpty(pno)) {
                            var h = '<a href="javascript:showFlow(';
                            h += "'" + pinstId + "'";
                            h += ",'" + audStsCd + "'";
                            h += ",'" + dataId + "'";
                            h += ')">' + pno + '</a>';
                            return h;
                        }
                    } else if (rowdata && rowdata.sysFlowSHDTOList && rowdata.sysFlowSHDTOList.length > 1) {
                        var h = "";
                        for (var i = 0; i < rowdata.sysFlowSHDTOList.length; i++) {
                            if (i != 0) {
                                h += '</br>';
                            }
                            var pno = rowdata.sysFlowSHDTOList[i].pno;
                            var audStsCd = rowdata.sysFlowSHDTOList[i].audStsCd;
                            var audStsCdNm = rowdata.sysFlowSHDTOList[i].audStsCdNm;
                            var pinstId = rowdata.sysFlowSHDTOList[i].pinstId;
                            var dataId = rowdata.sysFlowSHDTOList[i].dataId;
                            h += '<a href="javascript:showFlow(';
                            h += "'" + pinstId + "'";
                            h += ",'" + audStsCd + "'";
                            h += ",'" + dataId + "'";
                            h += ')">' + pno + '</a>';
                            h += "-";
                            h += audStsCdNm;
                        }
                        return h;
                    }
                }
                },
                getOptColumn()
            ],
            pageSize: 10,
            root: 'rows',
            record: 'total',
            hideLoadButton: true,
            width: '100%',
            height: '100%',
            heightDiff: -2,
            //isScroll: false,
            fixedCellHeight: false,
            checkbox: true,
            rownumbers: true
        });
        grid = liger.get("ligergrid1");
        grid.setHeight(500);
        $("#searchBar").ligerForm();

        $("#searchBtn").click(function (event) {
            var form = new liger.get("searchBar");
            var data = form.getData();
            data["lnkCompNm"] = $("#lnkCompNm").val();
            data["audStsCd"] = $("#audStsCd").val();
            /*   data["orgCd"] = $("#orgCd").val(); */
            for (name in data) {
                grid.setParm(name, data[name]);
            }
            ;
            grid.reload();
        });

        $("#pageloading").hide();

    });

    var ligerForm;

    function addBefore() {
        $.ligerDialog.SQopen({
            width: 800,
            top: 100,
            buttons: [
                {
                    text: "确定", cls: 'l-dialog-btn-ok', onclick: function () {
                    checkIsOk();
                }
                }, {
                    text: "取消", cls: 'l-dialog-btn-no', onclick: function () {
                        $.ligerDialog.hide();
                    }
                }
            ],
            title: '新增',
            target: $("#div_lnk_comp")
        });
        ligerForm = $("#div_lnk_comp_form").ligerForm({
            validate: true
        });

        //清空字段值
        resetForm(ligerForm);
    }

    function resetForm(ligerForm) {
        ligerForm.setData({
            lnkCompNmBk: "",
            orgCode: "",
            bizLicNo: "",
            taxRegNo: ""
        });
    }

    //检查是否存在公司信息
    function checkIsOk() {
        if (!ligerForm.valid()) {
            UIUtils.alertWarning(LogUtils.LOG_CMM_MUST_FIX_ITEM_INFO);
            return false; //验证不通过，不执行以下代码
        }
        var data = ligerForm.getData();
        data["lnkCompNm"] = data.lnkCompNmBk;
        data["orgCode"] = data.orgCode;
        data["bizLicNo"] = data.bizLicNo;
        data["taxRegNo"] = data.taxRegNo;

        var _url = '<%=path%>/project/prjlnkcomp/getExistComp.do';
        AjaxUtils.ajaxGet(_url, data, function (msg) {
            if (msg.success == 3) {//修改
                $.ligerDialog.SQconfirm(msg.info, function (flag) {
                    if (flag == true) {
                        var compId = msg.data.id;
                        parent.mainApp.f_addTab('tab_prj_lnk_comp_add' + compId, '修改挂靠公司', '<%=path%>/project/prjlnkcomp/toModifyPage.do?id=' + compId);
                        $.ligerDialog.hide();//关闭窗口
                    }
                });
            } else if (msg.success == 1) {//新增
                var param = "?lnkCompNm=" + data.lnkCompNmBk + "&orgCode=" + data.orgCode + "&bizLicNo=" + data.bizLicNo
                    + "&taxRegNo=" + data.taxRegNo;
                var timestamp = (new Date()).valueOf();
                parent.mainApp.f_addTab('tab_prj_lnk_comp_add' + timestamp, '新增挂靠公司', '<%=path%>/project/prjlnkcomp/toModifyPage.do' + param);
                $.ligerDialog.hide();//关闭窗口
            } else if (msg.success == 2) {
                UIUtils.alertError(msg.info);
            }
        }, true);
    };

    //显示流程
    function showFlow(_pInstId, _audStsCd, _businessKey) {
        if (StringUtils.isEmpty(_pInstId)) {
            UIUtils.alertWarning(CommonConstant.C_P_INSTANCEID_NOT_HERE);
            return;
        }
        var _formUrl = "/project/prjlnkcomp/toDetailPage.do?id=" + _businessKey + "@@processInstanceId=" + _pInstId + "@@projectId=";
        var params = "?pInsId=" + _pInstId + "&status=" + _audStsCd + "&businessKey=" + _businessKey + "&formUrl=" + _formUrl;
        Add_navTab('tab_prj_lnk_comp_flow_' + _businessKey, '查看流程', "<%=path%>/flow/flow/toFlowShowPageFowBus.do" + params);
    }

    //获取操作列
    function getOptColumn() {
        var column = {
            display: '操作',
            isSort: false,
            width: 250,
            align: 'left',
            render: function (rowdata, rowindex, value) {
                var h = "";
                //if(rowdata.audStsCd==CommonConstant.C_AUD_NO_HAPPEN_CODE){
                h += '<a class="sq-button-hasicon" href="javascript:modifyBtn(';
                h += "'" + rowdata.id + "'";
                h += ')"><i class="sqBtnIco_4"></i>修改</a>'
                h += '<a class="sq-button-hasicon" href="javascript:removeBtn(';
                h += "'" + rowdata.id + "'";
                h += ')"><i class="sqBtnIco_7"></i>删除</a>'
                //	 }
                var m = "";
                m += '<a class="sq-button-hasicon" href="javascript:modifyZKBtn(';
                m += "'" + rowdata.id + "',";
                m += "'" + rowdata.audStsCd + "'";
                m += ')"><i class="sqBtnIco_4"></i>变更</a>'
                var cr = "";
                cr += '<a class="sq-button-hasicon" href="javascript:linkCrAmtBtn(';
                cr += "'" + rowdata.id + "'";
                cr += ')"><i class="sqBtnIco_4"></i>敞口</a>'
                var isShowCmtBtn = $("#isShowCmtBtn");
                var isShowAudBtn = $("#isShowAudBtn");
                var isShowLinkCrAmtBtn = $("#isShowLinkCrAmtBtn"); 
                var end = "";
                //有修改删除权限 && 审核状态:待提交||审核不通过  -->修改删除显示
                if (isShowCmtBtn.length) {
                    if (rowdata.audStsCd == FlowConstant.FLOW_CHECK_STATE_NON || rowdata.audStsCd == FlowConstant.FLOW_CHECK_STATE_NO_PASS || rowdata.audStsCd == FlowConstant.FLOW_CHECK_STATE_RETURN_BACK) {
                        end += h;
                    }
                }
                if (rowdata.audStsCd == FlowConstant.FLOW_CHECK_STATE_PASS) {
                    end += m;
                } 
                if(rowdata.audStsCd == '99'){ //99 变更审核中
                	end += m;
                }
                end += cr;
                var w = "";
                if (buOrgCd == ProjectConstant.CODE_BU_ORG_CD_CAR) {
                    w += '<a class="sq-button-hasicon" href="javascript:toCompBankList(';
                    w += "'" + rowdata.id + "'";
                    w += ')"><i class="sqBtnIco_1"></i>挂靠公司账号</a>';
                }
                return end + w;
            }
        }
        return column;
    }

    function clearBtn() {
        var form = new liger.get("searchBar");
        form.setData({
            lnkCompNm: " ",
            audStsCd: " ",
        });
    }

    function modifyAudStsCd(id, audStsCd) {
        var data = {};
        data.id = id;
        data.audStsCd = audStsCd;
        var url = '<%=path%>/project/prjlnkcomp/modifyAudStsCd.do';
        var wait_dialog;
        $.ajax({
            type: 'post',
            dataType: 'json',
            data: data,
            url: url,
            beforeSend: function () {
                wait_dialog = top.$.ligerDialog.SQalert({content: LogUtils.LOG_CMM_DATA_LOADING, SQ_type: "waitting"});
            },
            success: function (msg) {
                wait_dialog.close();
                if (msg.success == 1) {
                    top.$.ligerDialog.SQalert({content: msg.info, SQ_type: "success"});
                    grid.reload();
                } else {
                    top.$.ligerDialog.SQalert({content: msg.info, SQ_type: "error"});
                }
            }
        });
    }

    function changeAudStsCd(id) {
        $.ligerDialog.SQopen({
            width: 500,
            height: 200,
            title: '审核信息',
            url: '<%=path%>/project/prjlnkcomp/toModifyAudStsCdPage.do?id=' + id,
            buttons: [{
                text: "保存", type: 'ok', cls: 'l-dialog-btn-ok',
                onclick: function (item, Dialog) {
                    if (Dialog.onOk) Dialog.onOk();
                }
            }, {
                text: "取消", type: 'cancel', cls: 'l-dialog-btn-no',
                onclick: function (item, Dialog) {
                    if (Dialog.onCancel) Dialog.onCancel();
                }
            }
            ]
        });
    }

    //格式化时间
    function renderDate(rowdata, index, value) {
        var dateStr = "";
        if (null != value) {
            var date = new Date(value);
            dateStr = DateUtils.formatDate(date, DateUtils.YYYY_MM_DD);
        }
        return dateStr;
    }

    function toCompBankList(_id) {
        if (!_id) return;
        Add_navTab('PrjComp_4' + _id, '挂靠公司账号', "<%=path%>/project/prjLnkCompBkAcc/toListPage.do?compId=" + _id);
    }

    //挂靠公司详情
    function detailBtn(id) {
        parent.mainApp.f_addTab('tab_prj_lnk_comp_detail' + id, '挂靠公司详情', '<%=path%>/project/prjlnkcomp/toDetailPage.do?id=' + id);
    }

    //添加挂靠公司
    function addBtn() {
        var callback = function () {
            grid.reload();
        }
        var timestamp = new Date().getTime();
        Add_navTab('tab_prj_lnk_comp_add' + timestamp, '添加挂靠公司', '<%=path%>/project/prjlnkcomp/toAddPage.do', callback);
    }

    //修改挂靠公司详情
    function modifyBtn(id) {
        var callback = function () {
            grid.reload();
        }
        Add_navTab('tab_prj_lnk_comp_add' + id, '修改公司详情', '<%=path%>/project/prjlnkcomp/toModifyPage.do?id=' + id, callback);
    }
    
    function linkCrAmtBtn(id) {
    	 var callback = function () {
             grid.reload();
         }
         Add_navTab('tab_prj_lnk_comp_LinkCrAmt' + id, '敞口', '<%=path%>/project/prjlnkcomp/toModifyPage.do?LinkCrAmt=1&id=' + id, callback);
    }

    function modifyZKBtn(id, audStsCd) {
        var callback = function () {
            grid.reload();
        }
        Add_navTab('tab_prj_lnk_comp_add' + id, '修改公司详情', '<%=path%>/project/prjLnkCompApply/toModifyPage.do?id=' + id + '&&audStsCd=' + audStsCd , callback);
    }


    //删除所选挂靠公司
    function removeBtn() {
        var selectedRows = grid.getSelectedRows();
        if (selectedRows.length < 1) {
            $.ligerDialog.SQalert({content: LogUtils.LOG_CMM_SELECT_ONLY_A_ROW, SQ_type: "warring"});
            return;
        }
        var _ids = [];
        for (var index in selectedRows) {
            _ids.push(selectedRows[index].id);
        }
        $.ligerDialog.confirm(LogUtils.LOG_CMM_DELETE_CONFIRM, function (flag) {
            if (flag == false) return;
            $.ajax({
                type: 'post',
                dataType: 'json',
                data: {ids: JSON.stringify(_ids)},
                url: '<%=path%>/project/prjlnkcomp/removeByIds.do',
                success: function (msg) {
                    if (msg.success == 1) {
                        grid.reload();
                    } else {
                        top.$.ligerDialog.SQalert({content: msg.info, SQ_type: "error"});
                    }
                }
            });
        });
    }

    function flowStartBtn() {
        var selectedRows = grid.getSelectedRows();
        if (selectedRows.length != 1) {
            $.ligerDialog.SQalert({content: LogUtils.LOG_CMM_SELECT_ONLY_A_ROW, SQ_type: "warning"});
            return;
        }
        var audStsCd = selectedRows[0].audStsCd;
        if (audStsCd == "1" || audStsCd == "4") {
            $.ligerDialog.SQalert({content: "审核中,不允许多次发起流程.", SQ_type: "warning"});
            return;
        } else if (audStsCd == "2") {
            $.ligerDialog.SQalert({content: "审核通过,不允许再次发起流程.", SQ_type: "warning"});
            return;
        }
        var url = null;
        var prjLnkCompId = selectedRows[0].id;
        if (buOrgCd == ProjectConstant.CODE_BU_ORG_CD_ZK || buOrgCd == ProjectConstant.CODE_BU_ORG_CD_ZC) {
            url = '<%=path%>/project/prjlnkcomp/addZkFlowStart.do?prjLnkCompId=' + prjLnkCompId;
        } else {
            url = '<%=path%>/project/prjlnkcomp/addFlowStart.do?prjLnkCompId=' + prjLnkCompId;
        }

        $.ligerDialog.confirm(FlowConstant.FLOW_ADD_START_CONFIRM, function (flag) {
            if (flag == false) return;
            var wait_dialog = $.ligerDialog.SQalert({
                content: LogUtils.LOG_CMM_DATA_LOADING,
                SQ_type: "waitting",
                onClose: function () {
                }
            });
            $.ajax({
                type: 'post',
                dataType: 'json',
                url: url,
                success: function (msg) {
                    wait_dialog.close();
                    if (msg.success == 1) {//成功发起流程
                        UIUtils.alertSuccessHold1(msg.info);
                        grid.reload();
                    } else if (msg.success == 0) {//错误
                        UIUtils.alertErrorHold1(msg.info);
                    }

                }
            });
        });

    }

    var loadedTabs = [];

    //初始化Tab页签
    function initTab() {
        pageInTab = $("#pageInTab").ligerTab({
            height: 'auto',
            contextmenu: false,
            minIframeHeight: GET_minIframeHeight(),
            onBeforeSelectTabItem: function (tabid) {
                var selectedRows = grid.getSelectedRows();
                if (tabid != "tabA") {
                    if (selectedRows.length != 1) {
                        $.ligerDialog.SQalert({content: LogUtils.LOG_CMM_SELECT_ONLY_A_ROW, SQ_type: "warning"});
                        return false;
                    }
                }
            },
            onAfterSelectTabItem: function (tabid) {
                var selectedRows = grid.getSelectedRows();
                if (tabid != "tabA") {
                    if (selectedRows.length != 1) {
                        $.ligerDialog.SQalert({content: LogUtils.LOG_CMM_SELECT_ONLY_A_ROW, SQ_type: "warning"});
                        return false;
                    } else {
                        if (loadedTabs.indexOf(selectedRows[0].id) < 0) {
                            loadedTabs = [];
                            loadedTabs.push(selectedRows[0].id);
                            switch (tabid) {
                                case "tabB":
                                    pageInTab.setTabItemSrc(tabid, "<%=path%>/project/prjlnkcomp/toPrjLinkFlowPage.do?linkId=" + selectedRows[0].id);
                                    break;
                                <%-- case "tabC":
                                    pageInTab.setTabItemSrc(tabid, "<%=path%>/project/prjLnkCompApply/prjCompApplyProcessPage.do?linkId=" + selectedRows[0].id);
                                    break;  --%>   
                            }
                        }
                        SetIframe_height();
                    }
                }
            }
        });
        $.extend(true, pageInApp, {
            pageInTab: pageInTab
        });
    };
</script>

</body>
</html>